stage.set_axis(250)
pause_clicked = False
def hover():
x = stage.mouse_x()
y = stage.mouse_y()
if (x <= -135 and x >= -250) and (y <= -185 and y >= -250):
click_box.set_opacity(.7)
click_text.set_opacity(.7)
else:
click_box.set_opacity(1)
click_text.set_opacity(1)
def pause():
global pause_clicked
while not pause_clicked:
stage.wait(0.1)
pause_clicked = False
def pause_click(sprite):
global pause_clicked
pause_clicked = True
def hide_click():
click_box.hide()
click_text.hide()
def show_click():
click_box.show()
click_text.show()
def delete_click(sprite):
pass
def delete_mouse_move():
pass
def delete_interval():
pass
def delete_collision(sprite, hit_sprite):
pass
def unpause():
pause_clicked = True
def pause_box():
global pause_clicked
show_click()
while not pause_clicked:
stage.wait(0.1)
pause_clicked = False
hide_click()
def else_statement():
stage.wait(3)
sprite.say("What if we want something to happen if the butterfly is far?", 3)
sprite.say("We need an else statement!")
else_text = codesters.Text("else:", -200, 94)
sprite_text = codesters.Text("butterfly", -165, 63, "orange")
equals2_text = codesters.Text(".", -125, 63)
codester_text = codesters.Text("turn_right(", -75, 63)
user_choice_clone2 = codesters.Text("360", -11, 63, "cornflowerblue")
paren_text = codesters.Text(")", 10, 63)
stage.wait(2)
x1 = 0
y1 = -180
sprite = codesters.Sprite("person1", x1, y1)
x2 = random.randint(30,240)
y2 = random.randint(-180,-60)
butterfly = codesters.Sprite("butterfly", x2, y2)
butterfly.set_size(.5)
delta_x = x2 - x1
delta_y = y2 - y1
distance = math.sqrt((delta_x**2) + (delta_y**2))
round = round(distance)
distanced = str(round)
sprite.say("Let's learn about if statements!")
stage.wait(2)
click_box = codesters.Rectangle(-203, -219, 125, 62.5, "green", "black")
click_text = codesters.Text("Continue", -197, -220, "white")
click_box.event_click(pause_click)
stage.event_mouse_move(hover)
pause_box()
sprite.say("We just created a butterfly at a random location and caculated the distance to the girl.")
distance_text = codesters.Text("distance", -187, 187, "orange")
equals_text = codesters.Text("=", -135, 187)
distance_value_text = codesters.Text(distanced, -100, 187, "cornflowerblue")
stage.wait(3)
pause_box()
sprite.say("We want the program to do something different depending on if the butterfly is close or far away.")
stage.wait(3)
pause_box()
sprite.say("To do this, we need if statements!")
stage.wait(2)
pause_box()
sprite.say("Look at the above code.")
if_text = codesters.Text("if", -217, 156)
distance_text_clone = codesters.Text("distance", -165, 156, "orange")
boolean_text = codesters.Text("<=", -110, 156)
test_value = codesters.Text("150", -75, 156, "cornflowerblue")
colon_text = codesters.Text(":", -55, 156)
sprite_text = codesters.Text("butterfly", -165, 125, "orange")
equals2_text = codesters.Text(".", -125, 125)
codester_text = codesters.Text("glide_to(", -80, 125)
user_choice_clone2 = codesters.Text("140, 130", 0, 125, "cornflowerblue")
paren_text = codesters.Text(")", 45, 125)
stage.wait(2)
pause_box()
sprite.say("Click on the highlighted part to learn more!")
highlight_box = codesters.Rectangle(-216, 156, 27, 31, "yellow")
highlight_box.set_opacity(.2)
highlight_box.event_click(pause_click)
pause()
sprite.say("This starts our if statement!")
highlight_box.hide()
stage.wait(2)
sprite.say("Click on the highlighted part to learn more!")
highlight_box.go_to(-168.75, 156)
highlight_box.set_width(distance_text_clone.get_width())
highlight_box.show()
pause()
sprite.say("This is called our 'test variable.'")
highlight_box.hide()
stage.wait(2)
sprite.say("It stores the input we're testing.")
stage.wait(2)
sprite.say("Click on the highlighted part to learn more!")
highlight_box.go_to(-75, 156)
highlight_box.set_width(test_value.get_width())
highlight_box.show()
pause()
sprite.say("This is called our 'test value.'")
highlight_box.hide()
stage.wait(2)
sprite.say("It's what we're comparing our test variable to.")
stage.wait(2)
sprite.say("Click on the highlighted part to learn more!")
highlight_box.go_to(-47, 125)
highlight_box.set_width(310)
highlight_box.show()
pause()
sprite.say("This is called our 'indented code block.'")
highlight_box.hide()
stage.wait(2)
sprite.say("It's the code underneath that ONLY runs if the if statment is true.")
stage.wait(3)
pause_box()
sprite.say("Time for a test!")
stage.wait(1.5)
answer = sprite.ask("Is the above if statement true?").lower().replace(" ", "")
close = False
if distance <= 150:
close = True
while close is True and answer != "yes":
if answer == "no":
sprite.say("Hm... Try again!")
stage.wait(1)
else:
sprite.say("Answer either 'yes' or 'no'!")
stage.wait(1.5)
answer = sprite.ask("Is the above if statement true?").lower().replace(" ", "")
while close is False and answer != "no":
if answer == "yes":
sprite.say("Hm... Try again!")
stage.wait(1)
else:
sprite.say("Answer either 'yes' or 'no'!")
stage.wait(1.5)
answer = sprite.ask("Is the above if statement true?").lower().replace(" ", "")
if close is True:
sprite.say("That's right!")
stage.wait(2)
sprite.say("Therefore, the code underneath the if statement runs!")
butterfly.glide_to(140, 130)
else_statement()
sprite.say("Notice that nothing happens, because our if statement WAS true!\nTherefore our else statement did not run.")
if close is False:
sprite.say("That's right!")
stage.wait(2)
sprite.set_say_color("red")
sprite.say("Therefore, the code underneath the if statement DOES NOT run!")
else_statement()
butterfly.turn_right(360)
tester = TestManager()
tester.display_success_message("Great job!")